package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

    @Autowired
    private TrackInfoService trackInfoService;

    @Autowired
    private VodService vodService;

    /**
     * 将音频文件上传到腾讯云点播平台
     *
     * @param file 音频文件
     * @return {mediaFileId:"文件唯一标识",mediaUrl:"播放地址"}
     */
    @Operation(summary = "将音频文件上传到腾讯云点播平台")
    @PostMapping("/trackInfo/uploadTrack")
    public Result<Map<String, String>> uploadTrack(MultipartFile file) {
        // 需要返回的数据是媒体唯一标识和播放地址
        Map<String, String> mapResult = vodService.uploadTrack(file);
        return Result.ok(mapResult);
    }


	/**
	 * 保存声音
	 * @param trackInfoVo
	 * @return
	 */
    @Operation(summary = "保存声音")
    @PostMapping("/trackInfo/saveTrackInfo")
    // @Validated 注解用于参数校验，具体配置在 TrackInfoVo 类中
    public Result saveTrackInfo(@RequestBody @Validated TrackInfoVo trackInfoVo) {
        Long userId = 1L;    //	Long userId = AuthContextHolder.getUserId();
        trackInfoService.saveTrackInfo(userId, trackInfoVo);
        return Result.ok();
    }


//    @Operation(summary = "查询专辑声音分页列表")
//    @PostMapping("/trackInfo/findAlbumTrackPage/{albumId}/{page}/{limit}")
//    public Result<Page<TrackListVo>> getUserAlbumPage(
//            @PathVariable int page,
//            @PathVariable int limit,
//            @PathVariable  Long albumId){
//      //1.获取登录用户ID 从ThreadLocal工具类中获取 暂时硬编码为：1
//        Long userId = 1L;    //	Long userId = AuthContextHolder.getUserId();
//        albumInfoQuery.setUserId(userId);
//        //2.构建MP分页对象Page web层封装页码页大小
//        Page<AlbumListVo> pageInfo = new Page<>(page, limit);
//        //3.调用业务逻辑分页专辑 其余分页属性：总记录数，总页数，当前页数据到持久层查询完成封装
//        pageInfo = albumInfoService.getUserAlbumPage(pageInfo, albumInfoQuery);
//        return Result.ok(null);
//    }


    /**
     * 获取当前用户声音分页列表
     * @param page
     * @param limit
     * @param trackInfoQuery
     * @return
     */
    @Operation(summary = "获取当前用户声音分页列表")
    @PostMapping("/trackInfo/findUserTrackPage/{page}/{limit}")
    public Result<Page<TrackListVo>> getUserTrackByPage(
            @PathVariable int page,
            @PathVariable int limit,
            @RequestBody TrackInfoQuery trackInfoQuery){
        //1.获取登录用户ID 从ThreadLocal工具类中获取 暂时硬编码为：1
        Long userId = 1L;    //	Long userId = AuthContextHolder.getUserId();
        trackInfoQuery.setUserId(userId);
        //2.构建MP分页对象Page web层封装页码页大小
        Page<TrackListVo> pageInfo = new Page<>(page, limit);
        //3.调用业务逻辑分页专辑 其余分页属性：总记录数，总页数，当前页数据到持久层查询完成封装
        pageInfo = trackInfoService.getUserTrackByPage(pageInfo, trackInfoQuery);
        return Result.ok(pageInfo);
    }

    /**
     * 根据声音ID查询声音信息
     *
     * @param id
     * @return
     */
    @Operation(summary = "根据声音ID查询声音信息")
    @GetMapping("/trackInfo/getTrackInfo/{id}")
    public Result<TrackInfo> getTrackInfo(@PathVariable Long id) {
        TrackInfo trackInfo = trackInfoService.getById(id);
        return Result.ok(trackInfo);
    }

    /**
     * TODO 该接口登录才可访问
     * 修改声音信息
     * @param id
     * @param trackInfoVo
     * @return
     */
    @Operation(summary = "修改声音信息")
    @PutMapping("/trackInfo/updateTrackInfo/{id}")
    public Result updateTrackInfo(@PathVariable Long id, @RequestBody @Validated TrackInfoVo trackInfoVo){
        trackInfoService.updateTrackInfo(id, trackInfoVo);
        return Result.ok();
    }

    /**
     * TODO 该接口登录才可访问
     * 根据ID删除声音
     * @param id
     * @return
     */
    @Operation(summary = "根据ID删除声音")
    @DeleteMapping("/trackInfo/removeTrackInfo/{id}")
    public Result removeTrackInfo(@PathVariable Long id){
        trackInfoService.removeTrackInfo(id);
        return Result.ok();
    }
}

